<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/1 16:37
 * Modified Time: 2019/7/1 16:37
 * Version: 2.0
 */

namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class MemberCard extends Model
{
    use SoftDeletes;
    protected $table = 'member_card';

    protected $guarded = [];

    //关联消费记录
    public function memberCardFlow()
    {
        return $this->hasMany(MemberCardFlow::class,'card_id','id');
    }
    
    //关联卡类
    public function cardType()
    {
        return $this->hasOne(CardType::class,'id','type_id');
    }

    //关联用户
    public function member(){
        return $this->hasOne(Member::class,'id','member_id');
    }

    public function cardTypeService(){
    	return $this->hasMany(CardTypeService::class, 'type_id','type_id');
    }

    public function royalty()
    {
        return $this->hasOne(Royalty::class,'item_id','id')->where('type',2);
    }


    public static function getMemberCardNameById($memberId){
    	return self::where(['id'=>$memberId])
		    ->with('cardType:id,category,name,present,price')
		    ->select(['id','card_num','type_id','balance','count','name','mobile'])
		    ->first();
    }





}